package demo.util;


import org.junit.Test;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class LoggerDemo {

    private static final Logger LOGGER = Logger.getLogger(LoggerDemo.class.getName());
    private static final String APP_NAME = "";
    private static final String LOG_DIR = "/var/log";
    private static Map<String, Logger> metricLoggers = new ConcurrentHashMap<>();

    public static void main(String[] args) {
        LOGGER.info("This is an info message");
        LOGGER.warning("This is a warning message");
        LOGGER.severe("This is a severe message");
    }

    public static synchronized Logger getLogger(String metricType) throws IOException {
        if (metricLoggers.containsKey(metricType)) {
            return metricLoggers.get(metricType);
        }
        Logger logger = Logger.getLogger(metricType);
        logger.setLevel(Level.INFO);
        // 创建一个文件处理程序
        String fileName = String.format("%s-%s-log.out", metricType, APP_NAME);
        String path = LOG_DIR + "/trace/logs/";
        String filePath = path + fileName;
        System.out.println("落盘文件：" + filePath);

        // 创建 FileHandler
        int fileSize = 1024 * 1024 * 1024;
        FileHandler fileHandler = new FileHandler(filePath, fileSize, 2, true);

        // 创建自定义的格式化器
        Formatter customFormatter = new Formatter() {
            public String format(LogRecord record) {
                return record.getMessage() + System.lineSeparator();
            }
        };
        fileHandler.setFormatter(customFormatter);
        fileHandler.setLevel(Level.INFO);
        logger.addHandler(fileHandler);
        metricLoggers.put(metricType, logger);
        return logger;
    }

    @Test
    public void testLogger() {
        try {
            // 创建 FileHandler
            FileHandler fileHandler = new FileHandler("myapp.log", true);

            // 创建 ConsoleHandler
            ConsoleHandler consoleHandler = new ConsoleHandler();

            // 创建简单的格式化器
            SimpleFormatter formatter = new SimpleFormatter();

            // 为 handlers 设置格式化器
            fileHandler.setFormatter(formatter);
            consoleHandler.setFormatter(formatter);

            // 获取全局 logger 并配置
            Logger globalLogger = Logger.getLogger("");
            globalLogger.setLevel(Level.INFO); // 设置全局日志级别
            globalLogger.addHandler(fileHandler);
            globalLogger.addHandler(consoleHandler);

            // 配置特定的 logger
            LOGGER.setLevel(Level.FINE);

            // 添加过滤器
            LOGGER.setFilter(record -> !record.getMessage().contains("ignore"));

            // 使用配置后的 logger
            LOGGER.info("This is an info message");
            LOGGER.fine("This is a fine message");
            LOGGER.warning("This is a warning message");
            LOGGER.severe("This is a severe message");
            LOGGER.info("This message should be ignored ignore");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
